package com.woniuxy.joylifeshop.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.common.entity.ShopMerchantRepository;
import com.woniuxy.joylifeshop.service.MerchantRepositoryService;
import com.woniuxy.joylifeshop.vo.ViewObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
public class MerchantRepositoryController {

    @Autowired
    MerchantRepositoryService merchantRepositoryService;

    /**
    * @Author SooYoo
    * @Description 调拨入库商品
    * @Param
    * @Return
    **/
    @RequestMapping("/addRepository")
    public ViewObject addRepository (@RequestBody String information){
        //System.out.println(information);
        //初始化VO
        ViewObject viewObject = new ViewObject();
        try {
            //找到对象
            ShopMerchantRepository shopMerchantRepository = JSON.parseObject(information, ShopMerchantRepository.class);
            //增加调拨
            merchantRepositoryService.allotRepository(shopMerchantRepository);

            viewObject.setData(null);
            viewObject.setCount(1);
            viewObject.setCode(0);
            viewObject.setMsg("入库成功");

            return viewObject;
        } catch (Exception e) {
            e.printStackTrace();

            viewObject.setData(null);
            viewObject.setCount(1);
            viewObject.setCode(0);
            viewObject.setMsg("入库失败");

            return viewObject;
        }
    }

    /**
    * @Author SooYoo
    * @Description 删除一个仓储对象
    * @Param 
    * @Return 
    **/
    @RequestMapping("/deleteOneRepository")
    public ViewObject deleteOneRepository(@RequestBody String information){

        ViewObject viewObject = new ViewObject();

        try {
            merchantRepositoryService.deleteRepositoryByRepositoryId(information);

            viewObject.setData(null);
            viewObject.setCount(1);
            viewObject.setCode(0);
            viewObject.setMsg("删除仓储数据成功");

            return viewObject;
        } catch (Exception e) {
            e.printStackTrace();

            viewObject.setMsg("删除仓储数据失败");
            viewObject.setCode(0);
            viewObject.setCount(1);
            viewObject.setData(null);

            return viewObject;
        }
    }


    /**
    * @Author SooYoo
    * @Description 初始化仓储数据
    * @Param 
    * @Return 
    **/
    @RequestMapping("/initRepositoryByCondition")
    public ViewObject initRepositoryByCondition(@RequestBody HashMap<String,String> information){
        //从json中获取数据
        String jsonstr = information.get("jsonstr");
        ShopMerchantRepository shopMerchantRepository = JSON.parseObject(jsonstr, ShopMerchantRepository.class);
        //封装VO
        ViewObject viewObject = new ViewObject();
        //分页
        PageHelper.startPage(Integer.parseInt(information.get("page")),Integer.parseInt(information.get("limit")));
        //找到要查询的仓储对象
        List<ShopMerchantRepository> shopMerchantRepositories = merchantRepositoryService.readMerchantRepositoryByCondition(shopMerchantRepository);
        //分页2
        PageInfo pageInfo = new PageInfo(shopMerchantRepositories);
        List<PageInfo> pageInfos = new ArrayList<>();
        pageInfos.add(pageInfo);
        //配置vo
        viewObject.setData(pageInfos);
        viewObject.setCode(0);
        viewObject.setCount(new Long(pageInfo.getTotal()).intValue());
        viewObject.setMsg("查询成功");
        return viewObject ;
    }



}
